package nl.rainbowsheep.reisorg.empl.site.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import nl.rainbowsheep.reisorg.empl.site.authentication.BasicAuthInterceptor;
import nl.rainbowsheep.reisorg.empl.site.model.Customer;
import nl.rainbowsheep.reisorg.empl.site.model.BurritoUser;
import nl.rainbowsheep.reisorg.empl.site.viewmodel.CustomerViewmodel;
import nl.rainbowsheep.reisorg.empl.site.viewmodel.TripBookingViewmodel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * Service class for customer. Sends requests to Restful webservice and receives
 * requests from that webservice.
 */
@Service
public class CustomerService {

    /**
     * use slf4j logger during debugging
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerService.class);

    @Autowired
    UserService userService;

    @Value("${api.url}")
    String url;

    /**
     * Send request to restful webservice to create a customer
     *
     * @param customerViewmodel
     * @return saved customer
     */
    public CustomerViewmodel insertCustomerViewmodel(CustomerViewmodel customerViewmodel) throws RuntimeException {
        BurritoUser g = customerViewmodel.getBurritoUser();
        BurritoUser gebruikerSaved = userService.saveUser(g);
        if (gebruikerSaved == null) {
            return null;
        }
        customerViewmodel.setBurritoUser(gebruikerSaved);
        customerViewmodel.getCustomer().setBurritoUserId(gebruikerSaved.getId());
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.postForObject(url + "customer/save", customerViewmodel, CustomerViewmodel.class);
    }

    public Customer updateCustomer(Customer customer) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.postForObject(url + "customer/save", customer, Customer.class);
    }

    public Customer getCustomer(long id) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);

        return template.getForObject(url + "/customer/details/id/" + id, Customer.class);
    }

    public List<Customer> getSearchResult(Customer customer) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        try {
            return template.postForObject(url + "/customer/find", customer, List.class);
        } catch (RestClientException ex) {
            return null;
        }
    }

    public List<TripBookingViewmodel> getTripBookingViewmodelsByByCustomerId(long customerId) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        List<TripBookingViewmodel> tripbookingviewmodels = template.getForObject(url + "/trip/booking/allbookingviewmodels?customerid=" + customerId + "&" + getLocaleParam(), List.class);
        return tripbookingviewmodels;
    }
    
        /**
     * Get current locale (from cookie)
     *
     * @return string with locale param
     */
    private String getLocaleParam() {
        return "locale=" + LocaleContextHolder.getLocale().getLanguage();
    }


}
